package flst.ipiweb.persistance.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import flst.ipiweb.persistance.interfaces.IEnseigneDAL;
import flst.ipiweb.transverse.Enseigne;
import flst.ipiweb.transverse.IPIWebException;

public class EnseigneJdbcDAL implements IEnseigneDAL{


	public Enseigne getEnseigneById(int id) throws IPIWebException {

		Connection maconnection;
		Enseigne ens = new Enseigne();
		
		try {
			maconnection = JdbcUtils.getInstance().getConnection();
			PreparedStatement req = maconnection.prepareStatement("SELECT IDENSEIGNE, LIBELLE FROM ENSEIGNE WHERE IDENSEIGNE = ? ;");
			req.setInt(1, id);
			req.execute();

			ResultSet rs = req.getResultSet();
			while (rs.next()) {
					ens.setId(rs.getInt(1));
					ens.setLibelle(rs.getString(2));	
			}

			req.close();
			maconnection.close();
		} 
		catch (Exception e) {
			throw new IPIWebException(e.getMessage());
		}
		return ens;
	}
	
	public List<Enseigne> getAllEnseigne() throws IPIWebException{
		
		
		List<Enseigne> lens = new ArrayList<Enseigne>();
		Connection maconnection;

		try {
			maconnection = JdbcUtils.getInstance().getConnection();
			Statement req = maconnection.createStatement();
			String requeteselect = "SELECT IDENSEIGNE, LIBELLE FROM ENSEIGNE;";
			req.execute(requeteselect);

			ResultSet rs = req.getResultSet();
			while (rs.next()) {
					Enseigne ens = new Enseigne();
					ens.setId(rs.getInt(1));
					ens.setLibelle(rs.getString(2));
					lens.add(ens);
			}

			req.close();
			maconnection.close();
		} catch (Exception e) {
			throw new IPIWebException(e.getMessage());
		}
		return lens;
	
	}

}
